package cn.zmlio.mybatisdbchecker.ui;

import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;
import java.awt.*;
import java.io.File;

public class MainFrame extends JFrame {
    private JTextField warPathField;
    private JButton browseButton;
    private JTree fileTree;
    private JList<String> statementList;
    private JTextArea sqlArea;
    private JTextField jdbcUrlField;
    private JTextField driverField;
    private JTextField usernameField;
    private JPasswordField passwordField;

    public MainFrame() {
        setTitle("MyBatis DB Checker");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(1200, 800);
        setLocationRelativeTo(null);

        // 创建主面板，使用BorderLayout
        JPanel mainPanel = new JPanel(new BorderLayout());

        // 左侧面板
        JPanel leftPanel = createLeftPanel();
        mainPanel.add(leftPanel, BorderLayout.WEST);

        // 右侧面板
        JPanel rightPanel = createRightPanel();
        mainPanel.add(rightPanel, BorderLayout.CENTER);

        add(mainPanel);
    }

    private JPanel createLeftPanel() {
        JPanel leftPanel = new JPanel(new BorderLayout());
        leftPanel.setPreferredSize(new Dimension(300, 0));

        // WAR文件选择区域
        JPanel warPanel = new JPanel(new BorderLayout(5, 5));
        warPanel.setBorder(BorderFactory.createTitledBorder("WAR File"));
        warPathField = new JTextField();
        browseButton = new JButton("Browse");
        browseButton.addActionListener(e -> chooseWarFile());
        warPanel.add(warPathField, BorderLayout.CENTER);
        warPanel.add(browseButton, BorderLayout.EAST);

        // 文件树
        fileTree = new JTree(new DefaultMutableTreeNode("WAR Content"));
        JScrollPane treeScroll = new JScrollPane(fileTree);
        treeScroll.setBorder(BorderFactory.createTitledBorder("Mapper Files"));

        // 数据源配置区域
        JPanel dbPanel = new JPanel(new GridBagLayout());
        dbPanel.setBorder(BorderFactory.createTitledBorder("Database Configuration"));
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.insets = new Insets(5, 5, 5, 5);


        // JDBC URL
        gbc.gridx = 0;
        gbc.gridy = 0;
        dbPanel.add(new JLabel("JDBC URL:"), gbc);
        gbc.gridx = 1;
        jdbcUrlField = new JTextField();
//        jdbcUrlField.setSize(200, );
        jdbcUrlField.setPreferredSize(new Dimension(200, 20));
        dbPanel.add(jdbcUrlField, gbc);

        // JDBC Driver

        gbc.gridx = 0;
        gbc.gridy = 1;
        dbPanel.add(new JLabel("JDBC Driver:"), gbc);
        gbc.gridx = 1;
        driverField = new JTextField();
//        jdbcUrlField.setSize(200, );
        driverField.setPreferredSize(new Dimension(200, 20));
        dbPanel.add(driverField, gbc);

        // Username
        gbc.gridx = 0;
        gbc.gridy = 2;
        dbPanel.add(new JLabel("Username:"), gbc);
        gbc.gridx = 1;
        usernameField = new JTextField();
        dbPanel.add(usernameField, gbc);

        // Password
        gbc.gridx = 0;
        gbc.gridy = 3;
        dbPanel.add(new JLabel("Password:"), gbc);
        gbc.gridx = 1;
        passwordField = new JPasswordField();
        dbPanel.add(passwordField, gbc);

        // 组装左侧面板
        leftPanel.add(warPanel, BorderLayout.NORTH);
        leftPanel.add(treeScroll, BorderLayout.CENTER);
        leftPanel.add(dbPanel, BorderLayout.SOUTH);

        return leftPanel;
    }

    private JPanel createRightPanel() {
        JPanel rightPanel = new JPanel(new BorderLayout());

        // Statement ID列表
        statementList = new JList<>(new DefaultListModel<>());
        JScrollPane statementScroll = new JScrollPane(statementList);
        statementScroll.setPreferredSize(new Dimension(0, 200));
        statementScroll.setBorder(BorderFactory.createTitledBorder("Statement IDs"));


        // SQL显示区域
        sqlArea = new JTextArea();
        sqlArea.setEditable(false);
        JScrollPane sqlScroll = new JScrollPane(sqlArea);
        sqlScroll.setPreferredSize(new Dimension(0, 200));
        sqlScroll.setBorder(BorderFactory.createTitledBorder("SQL"));

        // 组装右侧面板
        rightPanel.add(statementScroll, BorderLayout.NORTH);
//        rightPanel.add(treeScroll, BorderLayout.CENTER);
        rightPanel.add(sqlScroll, BorderLayout.SOUTH);

        return rightPanel;
    }

    private void chooseWarFile() {
        JFileChooser fileChooser = new JFileChooser();
        fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
        fileChooser.setFileFilter(new javax.swing.filechooser.FileFilter() {
            public boolean accept(File f) {
                return f.isDirectory() || f.getName().toLowerCase().endsWith(".war");
            }

            public String getDescription() {
                return "WAR Files (*.war)";
            }
        });

        int result = fileChooser.showOpenDialog(this);
        if (result == JFileChooser.APPROVE_OPTION) {
            warPathField.setText(fileChooser.getSelectedFile().getAbsolutePath());
            // TODO: 触发WAR文件解析
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            MainFrame frame = new MainFrame();
            frame.setVisible(true);
        });
    }
} 